package cn.iocoder.yudao.module.biz.service.customer.serviceUnit.member;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.member.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.customer.serviceUnit.member.ServiceUnitMemberDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.customer.serviceUnit.member.ServiceUnitMemberMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 客户档案-服务单元-组员 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ServiceUnitMemberServiceImpl implements ServiceUnitMemberService {

    @Resource
    private ServiceUnitMemberMapper serviceUnitMemberMapper;

    @Override
    public Long createServiceUnitMember(ServiceUnitMemberSaveReqVO createReqVO) {
        // 插入
        ServiceUnitMemberDO serviceUnitMember = BeanUtils.toBean(createReqVO, ServiceUnitMemberDO.class);
        serviceUnitMemberMapper.insert(serviceUnitMember);

        // 返回
        return serviceUnitMember.getId();
    }

    @Override
    public void updateServiceUnitMember(ServiceUnitMemberSaveReqVO updateReqVO) {
        // 校验存在
        validateServiceUnitMemberExists(updateReqVO.getId());
        // 更新
        ServiceUnitMemberDO updateObj = BeanUtils.toBean(updateReqVO, ServiceUnitMemberDO.class);
        serviceUnitMemberMapper.updateById(updateObj);
    }

    @Override
    public void deleteServiceUnitMember(Long id) {
        // 校验存在
        validateServiceUnitMemberExists(id);
        // 删除
        serviceUnitMemberMapper.deleteById(id);
    }

    @Override
        public void deleteServiceUnitMemberListByIds(List<Long> ids) {
        // 删除
        serviceUnitMemberMapper.deleteByIds(ids);
        }


    private void validateServiceUnitMemberExists(Long id) {
        if (serviceUnitMemberMapper.selectById(id) == null) {
            throw exception(BIZ_CUSTOMER_ARCHIVE_SERVICE_UNIT_MEMBER_NOT_EXISTS);
        }
    }

    @Override
    public ServiceUnitMemberDO getServiceUnitMember(Long id) {
        return serviceUnitMemberMapper.selectById(id);
    }

    @Override
    public PageResult<ServiceUnitMemberDO> getServiceUnitMemberPage(ServiceUnitMemberPageReqVO pageReqVO) {
        return serviceUnitMemberMapper.selectPage(pageReqVO);
    }

}